package hot100;


/*
 * Author：江松
 * Date：2023/4/10 15:59
 *
 *
 二叉树的直径：
 1，递归计算，左子树右子树高度之和，取最大值（如果完成后取值l+r，忽略了其在子树中会出现直径更高的情况）
 */

public class Main543 {
      public class TreeNode {
     int val;
     TreeNode left;
     TreeNode right;
     TreeNode() {}
     TreeNode(int val) { this.val = val; }
     TreeNode(int val, TreeNode left, TreeNode right) {
         this.val = val;
         this.left = left;
         this.right = right;
     }
 }


    int res=Integer.MIN_VALUE;
    public int getDepth(TreeNode root){
          if(root==null)return 0;
          int l=getDepth(root.left);
          int r=getDepth(root.right);
          res=Math.max(res,l+r);
          return Math.max(l,r)+1;
    }

    public int diameterOfBinaryTree(TreeNode root) {
        if(root==null)return 0;
        getDepth(root);
        return res;
    }
}
